{extend name="../../base/view/common/base" /}
{block name="style"}
<style>
.layui-form-label{width:120px}
.layui-input-block{margin-left:150px}
</style>
{/block}
{block name="body"}
<div class="layui-form p-page">
    <form class="layui-form" lay-filter="caseForm">
        <input type="hidden" name="id" value="{$detail.id|default='0'}">
        {if $id == 0}
        <input type="hidden" name="case_code" value="">
        {else}
        <div class="layui-form-item">
            <label class="layui-form-label">案件编号：</label>
            <div class="layui-input-block">
                <input type="text" name="case_code" class="layui-input" placeholder="请输入案件编号" value="{$detail.case_code|default=''}">
            </div>
        </div>
        {/if}
        <div class="layui-form-item">
            <label class="layui-form-label">案件类型：</label>
            <div class="layui-input-block">
                <select name="case_type" lay-verify="required" lay-reqText="请选择案件类型" lay-search>
                    {volist name="case_types" id="v" key="k"}
                    <option value="{$v}"{if isset($detail.case_type) && $detail.case_type == $v} selected{/if}>{$v}</option>
                    {/volist}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">代理阶段：</label>
            <div class="layui-input-block">
                <select name="stage" lay-verify="required" lay-reqText="请选择案件阶段" lay-search>
                    {volist name="case_stage" id="v" key="k"}
                    <option value="{$v}"{if isset($detail.stage) && $detail.stage == $v} selected{/if}>{$v}</option>
                    {/volist}

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">案由：</label>
            <div class="layui-input-block">
                <select name="case_cause_id" lay-verify="required" lay-reqText="请选择案由" lay-search>
                    <option value="0">无</option>
                    {volist name="case_causes" id="v"}
                    <option value="{$v.id}"{if isset($detail.case_cause_id) && $detail.case_cause_id == $v.id} selected{/if}>{$v.name}</option>
                    {/volist}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">接案日期：</label>
            <div class="layui-input-block">
                <input type="text" name="entrust_date" class="layui-input" id="entrust_date" placeholder="请选择接案日期" value="{$detail.entrust_date|default=''}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">当前进度：</label>
            <div class="layui-input-block">
                <select name="status" lay-verify="required" lay-reqText="请选择案件状态" lay-search>
                    {volist name="case_status" id="v" key="k"}
                    <option value="{$v}"{if isset($detail.status) && $detail.status == $v} selected{/if}>{$v}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">管辖法院：</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input picker-diy" name="court_name" placeholder="请选择" readonly value="{$detail.court_name|default=''}">
                <input type="hidden" class="layui-input" name="court_id" value="{$detail.court_id|default=0}">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">管家：</label>
            <div class="layui-input-block">
                <select name="secondary_lawyer" lay-verify="required" lay-reqText="请选择管家" lay-search>
                    <option value="">请选择管家</option>
                    {volist name="secondaryLawyerList" id="vo"}
                    <option value="{$vo.id}"{if isset($detail.secondary_lawyer) && $detail.secondary_lawyer == $vo.id} selected{/if}>{$vo.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">关联委托合同</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input picker-oa" data-types="contract" name="contract_name" placeholder="请选择" readonly value="{$detail.contract_name|default=''}">		
				<input type="hidden" class="layui-input" name="contract_id" value="{$detail.contract_id|default=0}">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">案件描述：</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入案件描述" class="layui-textarea">{$detail.description|default=''}</textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开庭时间：</label>
            <div class="layui-input-block">
                <input type="text" name="session_time" class="layui-input" id="session_time" placeholder="请选择开庭时间" value="{$detail.session_time|default=''}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开庭处所：</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input picker-diy" name="session_court_name" placeholder="请选择" readonly value="{$detail.session_court_name|default=''}">
                <input type="hidden" class="layui-input" name="session_court_id" value="{$detail.session_court_id|default=0}">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="webform">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
{/block}

{block name="script"}
<script>
var moduleInit = ['tool','oaPicker'];
function gouguInit() {
    laydate = layui.laydate;
    form = layui.form,
    layer = layui.layer,
    tool = layui.tool,
    oaPicker = layui.oaPicker;


    // 日期选择器
    laydate.render({
        elem: '#entrust_date',
        format: 'yyyy-MM-dd'
    });

    // 开庭时间选择器
    laydate.render({
        elem: '#session_time',
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm:ss'
    });

    // 监听表单提交
    form.on('submit(webform)', function(data) {
            layer.confirm('确定提交吗？', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                var loadIndex = layer.load(2, {shade: [0.1, '#fff']});
                var submitUrl = '/lawcase/lawcase/' + ($('input[name="id"]').val() > 0 ? 'edit' : 'add');
                $.post(submitUrl, data.field, function(res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1000
                        }, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.reloadTable && parent.reloadTable();
                            parent.layui.table.reload('lawcaseTable');
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
            });
            return false;
        });
    $('body').on('click','.picker-diy',function(){
        let that = $(this), ids = [], titles=[], map = {};
        let callback = function(data){
            for ( var i = 0; i <data.length; i++){
                ids.push(data[i].id);
                titles.push(data[i].name);
            }
            that.val(titles.join(','));
            that.next().val(ids.join(','));
        };
        let opts = {
            "title": '选择管辖法院',
            "url": '/lawcase/api/get_court',
            "ids":"",
            "titles":"",
            "area": ['600px', '580px'],
            "cols":[{field: 'id',width: 80,title:'序号',align:'center'},{field:'name',title:'法院名称'}],
            "page": true,
            "add": '/lawcase/court/add'
        }

        try {
            oaPicker.picker(opts, 1, callback);
        } catch (e) {
            console.error('组件调用失败:', e);
            layer.msg('组件初始化失败');
        }
    });
};
</script>
{/block}

